package com.jams.library.fresco;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.core.ImagePipeline;
import com.facebook.imagepipeline.request.ImageRequest;
import com.lvman.configs.instrumentation.ConfigConstants;
import com.lvman.configs.instrumentation.InstrumentedDraweeView;
import com.lvman.configs.instrumentation.PerfListener;

/**
 * Created by jams on 2016/4/12.
 * ***
 * Tel: ***
 */
public class FrescoUtil {
    //加载网络图片
    public static void loadNetUrl(SimpleDraweeView sdv, String url) {
        if (TextUtils.isEmpty(url)) url = "www";
        sdv.setImageURI(Uri.parse(url));
    }

    //加载本地图片
    public static void loadFileUrl(SimpleDraweeView sdv, String path) {

        sdv.setImageURI(Uri.parse("file://" + path));
    }

    //加载Content provider
    public static void loadContentProvider(SimpleDraweeView sdv, String path) {
        sdv.setImageURI(Uri.parse("content://" + path));
    }

    //加载asset
    public static void loadAsset(Context context, SimpleDraweeView sdv, String name) {
        sdv.setImageURI(Uri.parse("asset://" + context.getApplicationContext().getPackageName() + "/" + name));
    }

    //加载res
    public static void loadRes(Context context, SimpleDraweeView sdv, int idName) {
        String url = "res://" + context.getApplicationContext().getPackageName() + "/" + idName;
        if (Uri.parse(url) != null) {
            sdv.setImageURI(Uri.parse(url));
        }
    }

    public static void loadImg(InstrumentedDraweeView img, String url) {
        if (TextUtils.isEmpty(url)) {
            url = "www";
        }
        img.initInstrumentation(url, new PerfListener());
        ImageRequest imageRequest = ConfigConstants.getImageRequest(img, url);
        DraweeController draweeController = ConfigConstants.getDraweeController(imageRequest, img);
        img.setController(draweeController);
    }


    public static void loadImgWithResize(InstrumentedDraweeView img, String url, int width, int height) {
        if (TextUtils.isEmpty(url)) {
            url = "www";
        }
        img.initInstrumentation(url, new PerfListener());
        ImageRequest imageRequest = ConfigConstants.getImageRequest(img, url, width, height);
        DraweeController draweeController = ConfigConstants.getDraweeController(imageRequest, img);
        img.setController(draweeController);
    }


    //清除缓存
    public static void clearCache() {
        ImagePipeline imagePipeline = Fresco.getImagePipeline();
        imagePipeline.clearMemoryCaches();
        imagePipeline.clearDiskCaches();

// combines above two lines
        imagePipeline.clearCaches();
    }

    public static void clearCache(Uri uri) {
        ImagePipeline imagePipeline = Fresco.getImagePipeline();
        imagePipeline.evictFromCache(uri);
    }
}
